package zhaomh.catgirl.listener;

import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.configuration.file.FileConfiguration;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import zhaomh.catgirl.zTools;
import zhaomh.catgirl.zConfig;

import java.util.List;
import java.util.Objects;

public class ChatListener implements Listener {

    @EventHandler
    void chat(AsyncPlayerChatEvent chatEvent) {
        zTools t = new zTools("&d[Z-ねこむすめ]",chatEvent.getPlayer());

        String msg = chatEvent.getMessage();

        List<String> list = t.getKeysAtPath(zConfig.data,"catgirls");

        boolean cat = false;

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals(t.getName())) {
                cat = true;
                msg = msg.replace(Objects.requireNonNull(zConfig.data.getString("catgirls." + t.getName() + ".owner")), "主人");
                break;
            }
        }

        String str;
        if (cat) {
            str = zConfig.dConfig.getString("catgirlmessage");
            msg = msg + "喵~";

        } else {
            str = zConfig.dConfig.getString("message");
        }
        str = PlaceholderAPI.setPlaceholders(t.getPlayer(),str);
        str = str.replace("&","§");
        str = str.replace("${player}",chatEvent.getPlayer().getName());
        str = str.replace("${message}",msg);

        chatEvent.setFormat(str);
    }

}
